package com.yuzhe.controller;

import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.yuzhe.utils.JSONResult;
import org.n3r.idworker.Sid;

import java.io.File;

public class UoloadController extends Controller {

    public void uploadImg() {
        try {
            UploadFile uploadFile = this.getFile();
            if (uploadFile!=null){
                File desFile = new File(uploadFile.getUploadPath(), Sid.next() + ".jpg");
                boolean b = uploadFile.getFile().renameTo(desFile);
                if (b){
                    renderJson(JSONResult.ok("upload/"+desFile.getName()));
                } else {
                    renderJson(JSONResult.ok("upload/"+uploadFile.getFileName()));
                }
            } else {
                renderJson(JSONResult.errorMsg("上传的图片为空"));
            }

        } catch (Exception e) {
        }
//        String desCatalogStr = uploadFile.getUploadPath() + File.separator + "banner";
//        File desFileCatalog = new File(desCatalogStr);
//        if (!desFileCatalog.isDirectory() && !desFileCatalog.exists()) {
//            desFileCatalog.mkdirs();
//        }
//
//        File desImgFile = new File(desFileCatalog, Sid.next() + ".jpg");
//        fileService.fileChannelCopy(uploadFile.getFile(), desImgFile);
//        uploadFile.getFile().delete();
//        renderJson(JSONResult.ok("/upload/banner/" + desImgFile.getName()));
    }

//
//    public void uploadHomeImg() {
//        UploadFile uploadFile = this.getFile();
//        FileService fileService = new FileService();
//        String desCatalogStr = uploadFile.getUploadPath() + File.separator + "home";
//        File desFileCatalog = new File(desCatalogStr);
//        if (!desFileCatalog.isDirectory() && !desFileCatalog.exists()) {
//            desFileCatalog.mkdirs();
//        }
//
//        File desImgFile = new File(desFileCatalog, Sid.next() + ".jpg");
//        fileService.fileChannelCopy(uploadFile.getFile(), desImgFile);
//        uploadFile.getFile().delete();
//        renderJson(JSONResult.ok("/upload/home/" + desImgFile.getName()));
//    }
//
//
//    public void uploadNewsImg() {
//        UploadFile uploadFile = this.getFile();
//        FileService fileService = new FileService();
//        String desCatalogStr = uploadFile.getUploadPath() + File.separator + "news";
//        File desFileCatalog = new File(desCatalogStr);
//        if (!desFileCatalog.isDirectory() && !desFileCatalog.exists()) {
//            desFileCatalog.mkdirs();
//        }
//
//        File desImgFile = new File(desFileCatalog, Sid.next() + ".jpg");
//        fileService.fileChannelCopy(uploadFile.getFile(), desImgFile);
//        uploadFile.getFile().delete();
//        renderJson(JSONResult.ok("/upload/news/" + desImgFile.getName()));
//    }

}
